package com.wawayaya.bookreader.jni;

import android.util.Log;

import com.wawayaya.util.TLog;

import java.io.File;
import java.io.UnsupportedEncodingException;

/**
 * 拼音 so lib加载
 */
public class Pinyin
{
	private static final String s_strName = "cklrd_soft_pinyin";
	
	private static int s_nLoad = -1;
	public static boolean initialize(String strPath)
	{
		if (s_nLoad >= 0)
			return (s_nLoad != 0);
		try
		{
			boolean bExist = false;
			String strFile = "";
			if (strPath != null && strPath.length() > 0)
			{
				strFile = strPath + "/lib" + s_strName + ".so";
				File f = new File(strFile);
				bExist = f.exists();
			}
			if (bExist)
				System.load(strFile);
			else
				System.loadLibrary(s_strName);
			s_nLoad = 1;
		}
		catch(UnsatisfiedLinkError e)
		{
			s_nLoad = 0;
			TLog.log("pinyin : initialize : " + e);
		}
		return (s_nLoad != 0);
    }
	
	public static native byte[] Convert( String str );
	
	public static String convertpinyin(String strInput)   {
        if (!Pinyin.initialize(null)){
            return null;
        }
        String showText = "";
        try {
            byte[] showPinyin = Pinyin.Convert(strInput);
            if (showPinyin != null){
                showText = new String(showPinyin, "GBK");
            }
        } catch (UnsupportedEncodingException e) {
            Log.d("pinyin", "encoding:" + e.getMessage());
        } catch (UnsatisfiedLinkError e) {
            Log.d("pinyin", "link:" + e.getMessage());
        }
        return showText;
    }
}
